package MainTest;

import java.util.*;

/**
 * Created by xuyi on 2017/2/13.
 */
public class TreePrintLevel {

    public class TreeNode {
        int val = 0;
        TreeNode left = null;
        TreeNode right = null;
        public TreeNode(int val) {
            this.val = val;
        }
    }

    public static void main(String[] args) {
        int[] arr = {12,76,35,22,16,48,90,46,9,40};

        TreeNode treeNode0 = new TreePrintLevel().new TreeNode(12);
        TreeNode treeNode1 = new TreePrintLevel().new TreeNode(76);
        TreeNode treeNode2 = new TreePrintLevel().new TreeNode(35);
        TreeNode treeNode3 = new TreePrintLevel().new TreeNode(22);
        TreeNode treeNode4 = new TreePrintLevel().new TreeNode(16);
        TreeNode treeNode5 = new TreePrintLevel().new TreeNode(48);

        treeNode0.left = treeNode1;
        treeNode0.right = treeNode2;
        treeNode1.left = treeNode3;
        treeNode1.right = treeNode4;
        treeNode2.left = treeNode5;

        new TreePrintLevel().bianli(treeNode0);

//        TreePrintLevel.Solution solution = new TreePrintLevel().new Solution();
//        solution.Print(treeNode0);
    }

    public void bianli(TreeNode treeNode) {
        if (treeNode == null) {
            return;
        }
        int val = treeNode.val;
        System.out.println(val);
        this.bianli(treeNode.left);
        this.bianli(treeNode.right);
    }

    public class Solution {
        ArrayList<ArrayList<Integer>> Print(TreeNode pRoot) {
            int level = -1;
            ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>();
            this.addArrLevel(pRoot, level, result);

            return result;
        }

        public void addArrLevel(TreeNode pRoot, int level, ArrayList<ArrayList<Integer>> result) {
            if (pRoot == null) {
                return;
            }
            int newLevel = level + 1;
            ArrayList<Integer> arrayList = new ArrayList<>();
            arrayList.add(pRoot.val);
            try {
                ArrayList<Integer> exsitLevel = result.get(newLevel);
                exsitLevel.addAll(arrayList);
            } catch (Exception e) {
                result.add(arrayList);
            }
            if (pRoot.left != null) {
                this.addArrLevel(pRoot.left, newLevel, result);
            }
            if (pRoot.right != null) {
                this.addArrLevel(pRoot.right, newLevel, result);
            }
        }
    }
}
